<?

/* UserController controller class.
 * This class contains the handling code, divided into action methods.
 *
 */ 
/* @author Udo Schroeter <udo.schroeter@gmail.com>
 * @license dual license: LGPL / commercial use license
 */ 

class MapController extends AGController
{
  function __construct($ctrName) 
  {
    parent::__construct($ctrName);
    $this->invokeModel();
  }

  function __init()
  {
    $this->model->character = &$this->character;
    $this->model->parent = &$this;
    $this->game = $this->getModel('game');
    $this->game->loadZone($this->character->charDS['c_location']);
    $this->currentZoneId = $this->game->zoneDS['z_key'];
    $this->zoneStats = TextToStringList($this->game->zoneDS['z_stats']);
    $this->model->loadMapState($this->character->charDS['c_key'], $this->currentZoneId);
  }

  function Index()
  {
    $this->pageTitle = $this->game->zoneDS['z_name'];
  }
  
  function _updateMapState()
  {
    if (isset($_REQUEST['exp']))
      $this->model->updateExploredMapState($_REQUEST['exp']);
    if (isset($_REQUEST['l']))
    {
      $coords = explode(':', $_REQUEST['l']);
      $this->model->mstats['xpos'] = $coords[0];
      $this->model->mstats['ypos'] = $coords[1];
    }
    $this->model->saveMapState();
  }
  
  function ajax_update()
  {
    $this->_updateMapState();  
    $config['site.pagetemplate'] = 'blank';
    $this->skipView = true;
  }
  
  function ajax_location()
  {
    $this->_updateMapState();  
    global $config;
    $config['site.pagetemplate'] = 'blank';
    $this->selectedMapLocation = $_REQUEST['id'];
  }
  
  function ajax_zone()
  {
    $this->_updateMapState();  
    global $config;
    $config['site.pagetemplate'] = 'blank';
    $this->selectedMapLocation = $_REQUEST['id'];
    $locationType = getDefault($this->game->zoneStats[$this->selectedMapLocation.'.type'], 'text');
    if ($locationType == 'zone')
    {
      $zoneId = getDefault($this->game->zoneStats[$this->selectedMapLocation.'.zone'], $this->selectedMapLocation);
      $this->character->charDS['c_location'] = $zoneId;
      $this->character->saveCharacter();
      $this->redirect('index', 'game');
    }
  }
}


?>
